home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xsharp21.zip / DEMO1.C < prev    next >
Text File  |  1992-07-04  |  7KB  |  180 lines

  1. /* Sample X-Sharp 3D animation program. Demonstrates ambient and diffuse
  2.    shading, with up to three spotlights and ambient shading of a white ball.
  3.    The ambient light is green, as are two of the spots; given the palette
  4.    set-up, which is optimized for pure primary colors by assigning 64 levels
  5.    to each primary, shading with all green looks very good. The third
  6.    spotlight is blue; when it's on and any other lighting source is on,
  7.    color quantization problems become apparent; because the palette has only
  8.    four levels of each primary for use in mixing, only very rough
  9.    approximations of mixed colors can be displayed.
  10.  
  11.    All C code tested with Borland C++ 3.0 in C compilation mode and the
  12.    small model. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <conio.h>
  17. #include <dos.h>
  18. #include "polygon.h"
  19.  
  20. #define FIXED_REPS   0   /* set to # of reps to stop after, or
  21.                             to 0 for free-running */
  22.  
  23. void DisplayInstructions(void);
  24.  
  25. void main()
  26. {
  27.    int Done = 0, i, c;
  28.    Object *ObjectPtr;
  29.    union REGS regset;
  30.    unsigned long Reps = 0;
  31.  
  32.    DisplayInstructions();  /* put up opening instruction screen */
  33.  
  34.    SetGraphicsMode();      /* set the screen to graphics mode */
  35.    InitializeObjectList(); /* set up the initial objects */
  36.    InitializeFixedPoint(); /* set up fixed-point data */
  37.    InitializeBalls();      /* set up the ball(s) and add them to the
  38.                               object list */
  39.    InitializePalette();    /* set up the palette for whatever color mapping
  40.                               the driver for this adapter uses */
  41.    InitializeLighting();   /* set the initial lighting conditions */
  42.  
  43.    /* Start off showing page 0 */
  44.    ShowPage(PageStartOffsets[DisplayedPage = 0]);
  45.  
  46.    /* Keep transforming the objects, drawing them to the undisplayed page,
  47.       and flipping the page to show them */
  48.    do {
  49.  
  50.       /* For each object, regenerate viewing info, if necessary */
  51.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  52.             i++, ObjectPtr = ObjectPtr->NextObject) {
  53.          if (ObjectPtr->RecalcXform || RecalcAllXforms) {
  54.             ObjectPtr->RecalcFunc(ObjectPtr);
  55.             ObjectPtr->RecalcXform = 0;
  56.          }
  57.       }
  58.       RecalcAllXforms = 0;
  59.  
  60.       CurrentPageBase =    /* select other page for drawing to */
  61.             PageStartOffsets[NonDisplayedPage = DisplayedPage ^ 1];
  62.  
  63.       /* For each object, clear the portion of the non-displayed page
  64.          that was drawn to last time, then reset the erase extent */
  65.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  66.             i++, ObjectPtr = ObjectPtr->NextObject) {
  67.          FillRectangleX(ObjectPtr->EraseRect[NonDisplayedPage].Left,
  68.             ObjectPtr->EraseRect[NonDisplayedPage].Top,
  69.             ObjectPtr->EraseRect[NonDisplayedPage].Right,
  70.             ObjectPtr->EraseRect[NonDisplayedPage].Bottom,
  71.             CurrentPageBase, 0);
  72.          ObjectPtr->EraseRect[NonDisplayedPage].Left =
  73.               ObjectPtr->EraseRect[NonDisplayedPage].Top = 0x7FFF;
  74.          ObjectPtr->EraseRect[NonDisplayedPage].Right =
  75.                ObjectPtr->EraseRect[NonDisplayedPage].Bottom = 0;
  76.       }
  77.  
  78.       /* Sort the objects so we can draw them back to front */
  79.       SortObjects();
  80.  
  81.       /* Draw all objects */
  82.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  83.             i++, ObjectPtr = ObjectPtr->NextObject)
  84.          ObjectPtr->DrawFunc(ObjectPtr);
  85.  
  86.       /* Flip to display the page into which we just drew */
  87.       ShowPage(PageStartOffsets[DisplayedPage = NonDisplayedPage]);
  88.  
  89.       /* Let the user control lighting and ball location */
  90.       while (kbhit()) {
  91.          switch(c = getch()) {
  92.             /* Esc to exit */
  93.             case 0x1B:
  94.                Done = 1;
  95.                break;
  96.             /* Toggle the on/off state of spot 0, 1, or 2 */
  97.             case '0':
  98.             case '1':
  99.             case '2':
  100.                if (GetSpotState(c - '0'))
  101.                   TurnSpotOff(c - '0');
  102.                else
  103.                   TurnSpotOn(c - '0');
  104.                break;
  105.             /* Toggle ambient shading on or off */
  106.             case 'B':
  107.             case 'b':
  108.                if (GetAmbientState())
  109.                   TurnAmbientOff();
  110.                else
  111.                   TurnAmbientOn();
  112.                break;
  113.             case 'A':
  114.             case 'a':
  115.                BallEvent |= MOVE_AWAY;
  116.                break;
  117.             /* Spin the ball around Y rather than X, or vice-versa */
  118.             case 'S':
  119.             case 's':
  120.                BallEvent |= FLIP_SPIN_AXIS;
  121.                break;
  122.             case 'T':
  123.             case 't':
  124.                BallEvent |= MOVE_TOWARD;
  125.                break;
  126.             case 0:     /* extended code */
  127.                switch (getch()) {
  128.                   case 0x4B:     /* ball movement events; record them */
  129.                      BallEvent |= MOVE_LEFT;
  130.                      break;
  131.                   case 0x4D:
  132.                      BallEvent |= MOVE_RIGHT;
  133.                      break;
  134.                   case 0x48:
  135.                      BallEvent |= MOVE_UP;
  136.                      break;
  137.                   case 0x50:
  138.                      BallEvent |= MOVE_DOWN;
  139.                      break;
  140.                   default:
  141.                      break;
  142.                }
  143.                break;
  144.             default:
  145.                break;
  146.          }
  147.       }
  148.  
  149.       /* Move and reorient each object */
  150.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  151.             i++, ObjectPtr = ObjectPtr->NextObject)
  152.          ObjectPtr->MoveFunc(ObjectPtr);
  153.  
  154.       /* Count loop passes */
  155.       Reps++;
  156. #if FIXED_REPS
  157.       if (Reps == FIXED_REPS) Done = 1;
  158. #endif
  159.  
  160.    } while (!Done);
  161.    /* Return to text mode and exit */
  162.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  163.    int86(0x10, ®set, ®set);
  164.    exit(1);
  165. }
  166.  
  167. void DisplayInstructions() {
  168.    printf("Control keys:\n");
  169.    printf(" 0, 1, 2: toggle spotlights 0, 1, and 2 on and off\n");
  170.    printf(" A      : move ball away from you\n");
  171.    printf(" B      : toggle ambient (background) light on and off\n");
  172.    printf(" S      : flip the spin axis from X to Y or Y to X\n");
  173.    printf(" T      : move ball toward you\n");
  174.    printf(" arrows : move ball up, down, left, right\n");
  175.    printf(" Esc    : exit\n");
  176.    printf("\nPress any key to begin...\n");
  177.    if (getch() == 0x1B) exit(0);
  178. }
  179.  
  180.